package com.aidong.carnival.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aidong.carnival.controllers.concerns.Failure;
import com.aidong.carnival.controllers.concerns.Result;
import com.aidong.carnival.controllers.concerns.Success;
import com.aidong.carnival.controllers.concerns.View;
import com.aidong.carnival.controllers.concerns.View.Base;
import com.aidong.carnival.entity.Activity;
import com.aidong.carnival.entity.Message;
import com.aidong.carnival.service.ActivityService;
import com.fasterxml.jackson.annotation.JsonView;

@RestController
@RequestMapping("/carnival")
public class ActivitiesController {
    @Autowired
    ActivityService actService;

    @JsonView(Base.class)
    @RequestMapping(value = "/activities", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Result list(@RequestParam(value = "list", defaultValue = "1") int listType) {
        List<Activity> list;
        switch (listType) {
            case 1:
                list = actService.single();
                break;
            case 2:
                list = actService.pack();
                break;
            case 3:
                list = actService.vip();
                break;
            default:
                list = new ArrayList<Activity>();
        }
        return new Success(list);
    }

    @JsonView(View.Base.class)
    @RequestMapping(value = "/activities", method = RequestMethod.POST)
    @ResponseBody
    public Result buy(@RequestParam(value = "list", defaultValue = "1") int listType,
                      @RequestParam(value = "id[]") String[] ids,
                      @RequestParam(value = "idongId", required = true) String idongId,
                      @RequestParam(value = "contactName", required = true) String contactName,
                      @RequestParam(value = "contactPhone", required = true) String contactPhone,
                      @RequestParam(value = "cityId", required = true) int cityId,
                      @RequestParam(value = "addressId", required = true) int addressId,
                      HttpServletRequest request) {
        try {
            Message ms = actService.addBuy(listType, ids, idongId, contactName, contactPhone, cityId, addressId, request);

            if (ms.getCode() == 0) {
                return new Failure(ms.getMessage());
            } else {
                return new Success(ms.getMap());
            }
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
            return new Failure("当前预约人数较多，请稍后重试!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Failure("系统异常");
        }
    }

}
